package com.z.controller;

import com.z.entity.User;
import com.z.service.RegisterService;
import com.z.utils.SaltUtils;
import com.z.vo.RegisterVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ProjectName: springboot-08-shiro
 * @Package: com.baizhi.controller
 * @ClassName: UserController
 * @author: Arisapoke
 * @date: 2021/2/3 11:39
 * @Version: 1.0
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private RegisterService registerService;

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.jsp";
    }

    /**
     * 处理身份认证
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login")
    public String login(String username, String password, String verifycode, HttpSession session) {
        System.out.println("===login...===");
//        //比较验证码
//        String code = (String) session.getAttribute("code");
        try {
//            if (code.equalsIgnoreCase(verifycode)){
//                //获取用户登录对象
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);

                    subject.login(token);
                    System.out.println("login success");
                    return "redirect:/index.jsp";

//            }else {
//                throw new RuntimeException("验证码错误");
//            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误");
            return "redirect:/login.jsp";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            System.out.println("密码错误");

        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

        return "redirect:/login.jsp";
    }

    /**
     * 用户注册
     *
     * @return
     */
    @RequestMapping("register")
    public String register(RegisterVo registerVo){
        System.out.println("==开始注册==");
        try {
            //1 创建一个注册的用户对象
            User user = new User();
            user.setUsername(registerVo.getUsername());
            user.setPassword(registerVo.getPassword());
//            //2 生成随机盐
//            String salt = SaltUtils.getSalt(8);
//            //3 保存随机盐
//            user.setSalt(salt);
//            //4 明文密码进行 md5 + salt + hash散列
//            Md5Hash md5Hash = new Md5Hash(registerVo.getPassword(), salt, 1024);
//            //5 保存加密后的密码
//            user.setPassword(md5Hash.toHex());
//            //6 插入数据库
            int register = registerService.register(user);
            System.out.println(register);
                return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/register.jsp";
        }
    }

    /**
     * 验证码方法
     */
//    @RequestMapping("getImage")
//    public void getImge(HttpSession session, HttpServletResponse response) throws IOException {
//        //生成验证码
//        String code = VerifyCodeUtils.generateVerifyCode(4);
//        //验证码放入session中
//        session.setAttribute("code",code);
//        //验证码存入图片
//        ServletOutputStream os = response.getOutputStream();
//        //给响应流添加响应类型
//        response.setContentType("image/gpg");
//        VerifyCodeUtils.outputImage(220,60,os,code);
//    }



}
